﻿using KPI.Api;
using KPI.Data;
using KPIClient.Views;
using System;
using System.IO;
using System.Security.Cryptography;
using WPF_TY_UI;

namespace KPIClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : TYWindow
    {
        public MainWindow()
        {
            ViewLogin view = new ViewLogin();
            if (view.ShowDialog() == false)
            {
                Environment.Exit(0);
                return;
            }
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;

            //// 应用id
            //string appid = "202305220944ZY5rcED8Be6";
            //// SM4应用秘钥
            //string appSecret = "Jjnn3c8px3Pd5B25HbpNQF7bPKmSWP6w";
            //byte[] key = System.Text.Encoding.UTF8.GetBytes(appSecret);
            //byte[] iv = System.Text.Encoding.UTF8.GetBytes(appid);
            //byte[] data = System.Text.Encoding.UTF8.GetBytes("8d4178807b347e4cf63b817ec77dd399344e6490bb32d914bc7171a6645b30d3");

            ////byte[] encryptedData = Encrypt(key, iv, data);
            ////Console.WriteLine("Encrypted Data: " + Hex.ToHexString(encryptedData));

            //byte[] decryptedData = SM4Encryption.Decrypt(key, iv, data);
            //Console.WriteLine("Decrypted Data: " + System.Text.Encoding.UTF8.GetString(decryptedData));
        }

        private async void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //var ret = await Run(() =>
            //{
            //    return QuesionNaireApi.GetQuestionNaireByUserAndOffice("1015", "45");
            //});

        }

        public static string DESEncrypt(string data, string key, string iv)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cst);
            sw.Write(data);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="data">解密数据</param>
        /// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>
        /// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>
        /// <returns></returns>
        public static string DESDecrypt(string data, string key, string iv)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(byEnc);
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
        }
    }
}
